package com.mooc.tree.bak.BinTree.exerice;

import com.mooc.tree.bak.BinTree.BinTreeNode;
import com.mooc.tree.bak.BinTree.BinaryTree;

/**
 * @Author zhang.xin
 * @Description: 问题描述；输出二叉树的叶子结点（二叉树的结构和BinaryTree中定义的结构相同）
 * @Date 2019/12/24
 */
public class PrintLeaves {

    /**
     * 遍历一颗二叉树，在其左子树和右子树都为空，则该结点为叶结点,
     * 本例中，采用先序遍历的方式，进行输出
     */
    public void preOrderPrintLeaves(BinTreeNode root) {
        if (root != null) {
            if (root.getLeft() == null && root.getRight() == null ) {
                System.out.print(root.getData() + " ");
            }
            preOrderPrintLeaves(root.getLeft());
            preOrderPrintLeaves(root.getRight());
        }
    }

    // 测试
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        // 建立遍历小节中的二叉树结构
        BinTreeNode root = tree.createBinaryTree();
        PrintLeaves test = new PrintLeaves();
        // D E H I
        test.preOrderPrintLeaves(root);
    }

}
